package org.oy.sealogger.parser;

import java.util.Observable;

/**
 * 
 * @author riku
 *
 */
public class GSASentence extends NMEA0183Sentence {

	private static final int AM_SELECTION = 1;
	private static final int FIX2D3D = 2;
	private static final int SATELLITE1_PRN = 3;
	private static final int SATELLITE2_PRN = 4;
	private static final int SATELLITE3_PRN = 5;
	private static final int SATELLITE4_PRN = 6;
	private static final int SATELLITE5_PRN = 7;
	private static final int SATELLITE6_PRN = 8;
	private static final int SATELLITE7_PRN = 9;
	private static final int SATELLITE8_PRN = 10;
	private static final int SATELLITE9_PRN = 11;
	private static final int SATELLITE10_PRN = 12;
	private static final int SATELLITE11_PRN = 13;
	private static final int SATELLITE12_PRN = 14;
	private static final int PDOP = 15;
	private static final int HDOP = 16;
	private static final int VDOP = 17;
	private static final int CHECKSUM = 18;
	
	public static enum SatellitePRN {
		SATELLITE_1_PRN(SATELLITE1_PRN),
		SATELLITE_2_PRN(SATELLITE2_PRN),
		SATELLITE_3_PRN(SATELLITE3_PRN),
		SATELLITE_4_PRN(SATELLITE4_PRN),
		SATELLITE_5_PRN(SATELLITE5_PRN),
		SATELLITE_6_PRN(SATELLITE6_PRN),
		SATELLITE_7_PRN(SATELLITE7_PRN),
		SATELLITE_8_PRN(SATELLITE8_PRN),
		SATELLITE_9_PRN(SATELLITE9_PRN),
		SATELLITE_10_PRN(SATELLITE10_PRN),
		SATELLITE_11PRN(SATELLITE11_PRN),
		SATELLITE_12_PRN(SATELLITE12_PRN);
		
		protected int index;
		
		private SatellitePRN(int index) { this.index = index; }
	}
	
	public GSASentence() {}
	
	public GSASentence(String sentence) {
		super(sentence);
	}
	
	/**
	 * Auto selection of 2D or 3D fix
	 * @return
	 */
	public char getAutoManualSelection() {
		if(dataFields == null || 
				dataFields[AM_SELECTION] == null || 
				dataFields[AM_SELECTION].trim().length() == 0) {
			return 'A';
		}
		
		return dataFields[AM_SELECTION].charAt(0);
	}
	
	/**
	 * Get 3D fix
	 * @return 3D fix from the GPS device, no fix if no value is available
	 */
	public int getFix2D3D() {
		if(dataFields == null ||
				dataFields[FIX2D3D] == null ||
				dataFields[FIX2D3D].trim().length() == 0) {
			return 1;
		}
		
		return Integer.parseInt(dataFields[FIX2D3D]);
	}
	
	/**
	 * Get PRN of the satellite used for fix.
	 * @param satellitePRNField
	 * @return
	 */
	public int getSatellitePRN(SatellitePRN sprn) {
		if(dataFields == null ||
				dataFields[sprn.index] == null ||
				dataFields[sprn.index].trim().length() == 0) {
			return 0;
		}
		
		return Integer.parseInt(dataFields[sprn.index]);
	}
	
	public double getPDOP() {
		if(dataFields == null ||
				dataFields[PDOP] == null ||
				dataFields[PDOP].trim().length() == 0)
			return 0.0;
		
		return Double.parseDouble(dataFields[PDOP]);
	}
	
	public double getHDOP() {		
		if(dataFields == null ||
				dataFields[HDOP] == null ||
				dataFields[HDOP].trim().length() == 0)
			return 0.0;
		
		return Double.parseDouble(dataFields[HDOP]);
	}
	
	public double getVDOP() {		
		if(dataFields == null ||
				dataFields[VDOP] == null ||
				dataFields[VDOP].trim().length() == 0)
			return 0.0;
		
		return Double.parseDouble(dataFields[VDOP]);
	}

	public String getChecksum() {
		if(dataFields == null ||
				dataFields[CHECKSUM] == null)
			return "";
		
		return dataFields[CHECKSUM];
	}
	
	public int countActiveSatellites() {
		int satellites = 0;
		
		if(dataFields == null)
			return 0;
		
		for(int i = SatellitePRN.SATELLITE_1_PRN.index; i < SatellitePRN.SATELLITE_12_PRN.index; ++i) {
			if(dataFields[i] != null && dataFields[i].length() > 0)
				++satellites;
		}
		
		return satellites;
	}
	
	@Override
	public String getXMLSentence() {
		// TODO Auto-generated method stub
		return null;
	}

	public void update(Observable o, Object arg) {
		// TODO Auto-generated method stub

	}

}
